﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Client.Forms
{
    public partial class KeyTopUpForm : Form
    {
        List<Control> layoutControl = new List<Control>();
        int start = 30;
        int gap = 10;
        int left = 35;

        public KeyTopUpForm(string us_id)
        {
            InitializeComponent();
            layoutControl.AddRange(new Control[] { userNamePanel, cardKeyPanel, friendPanel, yzmPanel, commitPanel });
            userTextBox.Text = us_id;
        }

        private void TopUpForm_Load(object sender, EventArgs e)
        {
            this.Text = "使用秘钥充值";

            int h = 0;
            for(int i = 0; i < layoutControl.Count; i++)
            {
                layoutControl[i].Location = new Point(left, start + h + i * gap);
                layoutControl[i].TabIndex = i;
                h += layoutControl[i].Height;
            }
        }

        private async void CommitButton_Click(object sender, EventArgs e)
        {
            if (cardKeyTextBox.Text.Length < 12)
            {
                MessageBox.Show("卡密有误。", "提示");
                cardKeyTextBox.Focus();
                return;
            }
            if(yzmTextBox.Text.Length < 6)
            {
                MessageBox.Show("验证码有误","提示");
                yzmTextBox.Focus();
                return;
            }
            if (friendTextBox.Text == userTextBox.Text)
            {
                MessageBox.Show("好友ID不能与充值账号相同。", "提示");
                return;
            }
            try
            {
                await DataCenter.DataCenter.TopUp(userTextBox.Text, cardKeyTextBox.Text, yzmTextBox.Text, friendTextBox.Text);
                //new Client().Show();
                MessageBox.Show("充值成功！", "提示");
                this.Hide();
            }
            catch (DataCenter.MyException.APIReturnException error)
            {
                if (error.Message.IndexOf('~') != -1)
                {
                    var msg = error.Message.Split('~')[1];
                    MessageBox.Show(msg, "提示");
                }
            }
        }

        private void CardKeyTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private async void GetYZMLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DataCenter.DataCenter.SendYZM(userTextBox.Text);
            getYZMLinkLabel.LinkClicked -= GetYZMLinkLabel_LinkClicked;
            string tmpText = getYZMLinkLabel.Text;
            string tmpAccName = getYZMLinkLabel.AccessibleName;
            yzmTextBox.Focus();
            for (int i = 60; i > 0; i--)
            {
                string str = i + "秒后重新获取";
                getYZMLinkLabel.Text = str;
                getYZMLinkLabel.AccessibleName = str;
                await Task.Delay(1000);
            }
            getYZMLinkLabel.LinkClicked += GetYZMLinkLabel_LinkClicked;
            getYZMLinkLabel.Text = tmpText;
            getYZMLinkLabel.AccessibleName = tmpAccName;
        }

        void TextBoxSelectAll(object sender, EventArgs e)
        {
            var s = (TextBox)sender;
            s.SelectAll();
        }
        void SpaceLimit(object sender, EventArgs e)
        {
            var s = (TextBox)sender;
            s.Text = System.Text.RegularExpressions.Regex.Replace(s.Text, @"\s", "");
            s.SelectionStart = s.Text.Length;
        }

        private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }
    }
}
